package com.example.test.chapter7.listviewoneicon;

import com.example.test.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDynamicActivity extends Activity implements
		OnItemClickListener {
	private TextView textView;
	private String[] SmartPhones = new String[] { "HTC Rezound",
			"Samsung Galaxy S II Skyrocket", "Samsung Galaxy Nexus",
			"Motorola Droid Razr", "Samsung Galaxy S", "Samsung Epic Touch 4G",
			"iPhone 4S", "HTC Titan" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_one_icon);
		ListView listView = (ListView) findViewById(R.id.list_view_one_icon);
		listView.setAdapter(new IconicAdapter());
		textView = (TextView) findViewById(R.id.selected_list_view_one_icon);
		listView.setOnItemClickListener(this);
	}

	class IconicAdapter extends ArrayAdapter<String> {
		IconicAdapter() {
			super(ListViewDynamicActivity.this, R.layout.row, SmartPhones);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = getLayoutInflater();
			View row = inflater.inflate(R.layout.row, parent, false);
			TextView label = (TextView) row
					.findViewById(R.id.label_list_view_one_icon);
			label.setText(SmartPhones[position]);

			ImageView icon = (ImageView) row.findViewById(R.id.icon_list_view);

			if (SmartPhones[position].startsWith("Samsung")) {
				icon.setImageResource(R.drawable.checkicon);
			} else {
				icon.setImageResource(R.drawable.radiobutton);
			}

			return (row);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		textView.setText(SmartPhones[arg2]);
	}
}
